import api from '@/store/api';

export const useSensorStore = defineStore('SensorStore', () => {
  const sensors = ref([] as any[]);
  const setSensors = (sensorsData: any[]) => {
    sensorsData.forEach((item: { name: any }) => {
      for (let i = 0; i < sensors.value.length; i++) {
        if (sensors.value[i].name === item.name) {
          sensors.value.splice(i, 1);
          break;
        }
      }
    });
    sensors.value = [...sensors.value, ...sensorsData];
  };
  const getAllSensors = async () => {
    const collection = await getChassisCollection();
    if (!collection) return;
    const promises = collection.reduce((acc: any[], id: any) => {
      acc.push(getSensors(id));
      acc.push(getThermalSensors(id));
      acc.push(getPowerSensors(id));
      return acc;
    }, []);
    return await api.all(promises);
  };
  const getChassisCollection = async () => {
    return await api
      .get('/redfish/v1/Chassis')
      .then(({ data: { Members } }) =>
        Members.map((member: { [x: string]: any }) => member['@odata.id']),
      )
      .catch((error) => console.log(error));
  };
  const getSensors = async (id: any) => {
    const sensors = await api
      .get(`${id}/Sensors`)
      .then((response) => response.data.Members)
      .catch((error) => console.log(error));
    if (!sensors) return;
    const promises = sensors.map((sensor: { [x: string]: string }) => {
      return api.get(sensor['@odata.id']).catch((error) => {
        console.log(error);
        return error;
      });
    });
    return await api.all(promises).then(
      api.spread((...responses: any) => {
        const sensorData = responses.map(
          ({ data }: { data: { [index: string]: any } }) => {
            return {
              name: data.Name,
              status: data.Status.Health,
              currentValue: data.Reading,
              lowerCaution: data.Thresholds?.LowerCaution?.Reading,
              upperCaution: data.Thresholds?.UpperCaution?.Reading,
              lowerCritical: data.Thresholds?.LowerCritical?.Reading,
              upperCritical: data.Thresholds?.UpperCritical?.Reading,
              units: data.ReadingUnits,
              type: data.ReadingType,
              memberId: data.Id,
            };
          },
        );
        setSensors(sensorData);
      }),
    );
  };
  const getThermalSensors = async (id: any) => {
    return await api
      .get(`${id}/Thermal`)
      .then(({ data: { Fans = [], Temperatures = [] } }) => {
        const sensorData: {
          name: any;
          status: any;
          currentValue: any;
          lowerCaution: any;
          upperCaution: any;
          lowerCritical: any;
          upperCritical: any;
          units: any;
          type: any;
          memberId: any;
        }[] = [];
        Fans.forEach(
          (sensor: {
            Name: any;
            Status: { Health: any };
            Reading: any;
            LowerThresholdNonCritical: any;
            UpperThresholdNonCritical: any;
            LowerThresholdCritical: any;
            UpperThresholdCritical: any;
            ReadingUnits: any;
            MemberId: any;
          }) => {
            let fanType = 'fan_pwm';
            if (sensor.ReadingUnits === 'RPM') {
              fanType = 'fan_tach';
            }
            sensorData.push({
              name: sensor.Name,
              status: sensor.Status.Health,
              currentValue: sensor.Reading,
              lowerCaution: sensor.LowerThresholdNonCritical,
              upperCaution: sensor.UpperThresholdNonCritical,
              lowerCritical: sensor.LowerThresholdCritical,
              upperCritical: sensor.UpperThresholdCritical,
              units: sensor.ReadingUnits,
              type: fanType,
              memberId: sensor.MemberId,
            });
          },
        );
        Temperatures.forEach(
          (sensor: {
            Name: any;
            Status: { Health: any };
            ReadingCelsius: any;
            LowerThresholdNonCritical: any;
            UpperThresholdNonCritical: any;
            LowerThresholdCritical: any;
            UpperThresholdCritical: any;
            MemberId: any;
          }) => {
            sensorData.push({
              name: sensor.Name,
              status: sensor.Status.Health,
              currentValue: sensor.ReadingCelsius,
              lowerCaution: sensor.LowerThresholdNonCritical,
              upperCaution: sensor.UpperThresholdNonCritical,
              lowerCritical: sensor.LowerThresholdCritical,
              upperCritical: sensor.UpperThresholdCritical,
              units: '℃',
              type: 'temperature',
              memberId: sensor.MemberId,
            });
          },
        );
        setSensors(sensorData);
      })
      .catch((error) => console.log(error));
  };
  const getPowerSensors = async (id: any) => {
    return await api
      .get(`${id}/Power`)
      .then(({ data: { Voltages = [] } }) => {
        const sensorData = Voltages.map(
          (sensor: {
            Name: any;
            Status: { Health: any };
            ReadingVolts: any;
            LowerThresholdNonCritical: any;
            UpperThresholdNonCritical: any;
            LowerThresholdCritical: any;
            UpperThresholdCritical: any;
            MemberId: any;
          }) => {
            return {
              name: sensor.Name,
              status: sensor.Status.Health,
              currentValue: sensor.ReadingVolts,
              lowerCaution: sensor.LowerThresholdNonCritical,
              upperCaution: sensor.UpperThresholdNonCritical,
              lowerCritical: sensor.LowerThresholdCritical,
              upperCritical: sensor.UpperThresholdCritical,
              units: 'V',
              type: 'voltage',
              memberId: sensor.MemberId,
            };
          },
        );
        setSensors(sensorData);
      })
      .catch((error) => console.log(error));
  };

  return {
    sensors,
    setSensors,
    getAllSensors,
    getChassisCollection,
    getSensors,
    getThermalSensors,
    getPowerSensors,
  };
});
